﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public class FLTEXGBMOVReport : BaseReport
    {
        string strCompany = "HLT";
        string strExcelFile = "";
        string sheetName = "";
        string Standard = "GBMOV";
        string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\HLT\HLTPlanningReport.xls");
        public FLTEXGBMOVReport()
            : base()
        {
        }
        public FLTEXGBMOVReport(Dictionary<string, object> dicParamater)
            : base(dicParamater)
        {
            strCompany = dicParamater["Company"].ToString().Trim();
            strExcelFile = dicParamater["ExcelFile"].ToString().Trim();
            strExcelFile = @"FLT\FLTInventory.xls";
            /*
             GMMANDANT,GMJJJJ ,GMMM ,GMTT ,GMLO ,GMLB ,GMGART ,GMGVAR ,GMGDES ,GMGCOLOR ,GMGCDES,
           GMPARTIE ,GMBATCH,GMSPPART ,GMFBPART ,GMZWART,GMSTARTQTY ,GMPOIN ,GMMUIN ,GMPDIN ,GMSPIN ,
           GMD7IN ,GMESIN ,GMRDOUT,GMCLOUT,GMRSOUT,GMADJIN,GMREIN ,GMORIN ,GMSTOUT,GMTOOUT,GMCSOUT,
           GMCRIN ,GMOSOUT,GMPDOUT,GMSPOUT,GMOCOUT,GMOSIN ,GMOROUT,GMSTARTDAT ,GMREMARK 
             */
            #region Excel  columns
            columns.Add("GMLO"); 
            columns.Add("GMGDES");
            columns.Add("GMGART");
            columns.Add("GMGVAR");
            columns.Add("GMGCOLOR");
            columns.Add("GMGCDES");
            columns.Add("GMPARTIE");
            columns.Add("GMSTARTQTY");
            columns.Add("GMOSIN");
            columns.Add("GMMUIN");
            columns.Add("GMADJIN");
            columns.Add("GMOCOUT");
            columns.Add("GMOROUT");
            columns.Add("GMENDQTY");
            //GMGART	GMGVAR	GMGCOLOR	GMPARTIE	GMSTARTQTY	GSEXPROUT	GMEXPIN	GMELSEIN	GMENDQTY

            #endregion 
        }
        public void FillInExcelData(ArrayList columns, DataSet setSoruce, int xStart = 7, int yStart = 1)
        {
            try
            {
                int x = xStart, y = yStart;
                DataTable dtSoruce = setSoruce.Tables["CN_GMBALANCE"];
                DataTable dtOutput = new DataTable();
                dtOutput.Columns.Add("GMLO", typeof(System.String));
                dtOutput.Columns.Add("GMGDES", typeof(System.String));
                dtOutput.Columns.Add("GMGART", typeof(System.String));
                dtOutput.Columns.Add("GMGVAR", typeof(System.String));
                dtOutput.Columns.Add("GMGCOLOR", typeof(System.String));
                dtOutput.Columns.Add("GMGCDES", typeof(System.String));
                dtOutput.Columns.Add("GMPARTIE", typeof(System.String));
                dtOutput.Columns.Add("GMSTARTQTY", System.Type.GetType("System.Decimal"));
                dtOutput.Columns.Add("GMOSIN", System.Type.GetType("System.Decimal"));
                dtOutput.Columns.Add("GMMUIN", System.Type.GetType("System.Decimal"));
                dtOutput.Columns.Add("GMADJIN", System.Type.GetType("System.Decimal"));
                dtOutput.Columns.Add("GMOCOUT", System.Type.GetType("System.Decimal"));
                dtOutput.Columns.Add("GMOROUT", System.Type.GetType("System.Decimal"));
                dtOutput.Columns.Add("GMENDQTY", System.Type.GetType("System.Decimal"));
                dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);

                exAgent.DataTableToExcel(dtOutput, 7, 1);
                int count = dtOutput.Rows.Count;
                exAgent.BatchConvert2Number(7, 8, count + 6, 14);
                exAgent.SetCellValue(1, 1, " ");
                /*foreach (DataRow dataRow in dtSoruce.Rows)
                {
                    y = yStart;
                    foreach (string column in columns)
                    {
                        exAgent.SetCellValue(x, y, dataRow[column].ToString());
                        y++;
                    }
                    x++;
                } */
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public void WriteExcelReport(string fileType, string STARTDATE, string ENDDATE)
        {
            try
            {
                string filename = "";
                string sheetName = "";
                string Standard = "EXGBMOV";
                strExcelFile = @"FLT\FLTStandard.xls";
                string filePath = Path.Combine(PathHelper.ApplicationPath, @"Report\" + strExcelFile);

                exAgent.Open(filePath);
                exAgent.Company = strCompany;
                exAgent.strExcelFile = strExcelFile;
                string filedate = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
                filename = @"Financial\委外收发存_" + filedate + ".xls";
                string file = Path.Combine(PathHelper.ApplicationPath, @"Report\" + filename);
                sheetName = DateTime.Now.ToShortTimeString().Replace(@":", "");
                if (System.IO.File.Exists(file))
                {
                    System.IO.File.Delete(file);
                }
                exAgent.CreateNewSheet(filename, Standard, sheetName);

                exAgent.SetCellValue(1, 3, STARTDATE + "-" + ENDDATE);
                FillInExcelData(columns, dsSource, 7, 1);
                base.WriteExcelReport(PrintType);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
